#include <iostream>
#include <vector>
#include <string>
#include <windows.h>
#include <cmath>

using namespace std;

// usage:
// ControlCpuUsageCurve.exe -x
// x = 1-9
// x = sin
//
int main(int argc, const char* argv[])
{
	if(argc != 2) 
	{
			cout << " 2 parameters are required!" << endl;
			return 1;
	}

	SetThreadAffinityMask(::GetCurrentThread(), 1);

	string arg = argv[1];
	if(arg == "sin")
	{
			const int kPeriod = 800;
			const int kInterval = 200;
			const int kHalfInterval = kInterval / 2;
			const float Pi = 3.14;

			int busyTime = 0;
			DWORD_PTR startTime = 0;
			while(true)
			{
					for (int i = 0; i < kPeriod; ++i)
					{
						startTime = ::GetTickCount();
						busyTime = kHalfInterval + sin(((i+1) * 2 * Pi) / kPeriod) * kHalfInterval;

						while (::GetTickCount() - startTime <= busyTime) {}

						::Sleep(kInterval - busyTime);
					}
			}
	}
	else
	{
			int level = arg[0] - '0';
			if(level > 9 || level < 1) return 1;

			int total = 200;
			int busyTime = total * level / 10;
			int idleTime = 200 - busyTime;
			DWORD_PTR startTime = 0;
			while(true)
			{
					startTime = ::GetTickCount();
					while(::GetTickCount() - startTime <= busyTime) {}
					
					::Sleep(idleTime);
			}
	}
	return 0;
}
